package com.bookshop.order.logic;


import com.bookshop.dataOperator.DatabaseUtils;
import com.bookshop.order.data.OrderDBData;
import com.bookshop.order.data.Search;
import com.bookshop.order.state.OrderState;
import com.bookshop.pojo.OrderPOJO;
import com.bookshop.tool.Format;
import com.bookshop.vo.OrdersVO;

public class OrderManagement implements OrderDBData{
	private final Search mSearch;
	
	public OrderManagement(){
		mSearch = new Search();
	}
	
	public boolean add(Order pOrder){
		OrderPOJO orderManagePOJO = pOrder.toPOJO();
		String order = "insert into " + TABLE_NAME + "(" + Format.formatColumns(COLUMNS) + ") values('" + 
				orderManagePOJO.id  + "','" + orderManagePOJO.user + "','" + orderManagePOJO.date + "','" + 
				orderManagePOJO.books + "','" + orderManagePOJO.state + "','" + orderManagePOJO.eqivalent + "','" +
				orderManagePOJO.discount + "','" + orderManagePOJO.price + "')";
		return DatabaseUtils.excute(order);
	}
	
	public boolean remove(String pId){
		String order = "DELETE FROM " + TABLE_NAME + " WHERE ID=" + pId;
		return DatabaseUtils.excute(order);
	}
	
	public boolean removeUser(String pUser){
		String order = "DELETE FROM " + TABLE_NAME + " WHERE USER='" + pUser + "'";
		return DatabaseUtils.excute(order);
	}
	
	public boolean modifyState(String pId,OrderState state){
		String order = "UPDATE orderiterm SET " + COLUMNS[INDEX_STATE]
				 + "='" + state + "' WHERE " + COLUMNS[INDEX_ID] + "='" + pId + "'";
		return DatabaseUtils.excute(order); 
	} 
	
	public OrdersVO list(int pBegin,int pEnd){
		OrdersVO ordersVO = mSearch.list(pBegin,pEnd);
		return ordersVO;
		
	}
	

	public OrdersVO searchByUser(String pAccount,int pBegin,int pEnd){
		return mSearch.byUser(pAccount,pBegin,pEnd);
	}

	public OrdersVO searchByState(OrderState pOrderState, int pBegin, int pEnd) {
		return mSearch.byState(pOrderState, pBegin, pEnd);
	}
}
